<template>
    <div class="header">
      <span class="title">招得快招投标信息服务平台</span>
      <el-input v-model="search" size="medium" class="el_inp" placeholder="请输入公司名称" prefix-icon="el-icon-search"></el-input>
      <el-button @click="goSearch" type="primary" size="medium" class="sous" icon="el-icon-search">搜索</el-button>
      <img v-if="imgUrl" :src="imgUrl" class="imgSize">
      <i v-else class="el-icon-user"></i>
      <el-button type="info" class="dengr" round @click="login">登入</el-button>
        <div class="layout">
          <router-link class="daohang" to="/home">首页</router-link>
          <router-link class="daohang" to="/successShow">招标历史</router-link>
          <router-link class="daohang" to="/zhaoBList">招标信息</router-link>
          <router-link class="daohang" to="/zhaoBInsert">招标发布</router-link>
          <router-link class="daohang" to="/user">个人中心</router-link>
          <router-link class="daohang" to="/juBao">举报</router-link>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      search: '',
      imgUrl: ''
    }
  },
  computed: {
    isLogin () {
      return this.$store.getters.token
    }
  },
  created () {
    const base64ToBlob = (base64) => {
      const arr = base64.split(',')
      const type = arr[0].match(/:(.*?);/)[1]
      const bstr = atob(arr[1])
      let n = bstr.length
      const u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new Blob([u8arr], { type })
    }
    const reader = localStorage.getItem('fileData')
    if (reader !== '' && reader !== null) {
      this.imgUrl = window.URL.createObjectURL(base64ToBlob(reader))
    }
  },
  methods: {
    goSearch () {
      if (this.search === '' || this.search.trim() === '') {
        return
      }
      this.$router.push(`/search/${this.search}`)
    },
    login () {
      this.$router.push('/login')
      if (this.isLogin) {
        this.$confirm('当前用户已登入，是否退出', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('user/logout')
          localStorage.removeItem('fileData')
          this.$message({
            type: 'success',
            message: '退出成功!'
          })
        }).catch(() => {
          this.$router.back()
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 10.5rem;
  background-color: #ebf9ff;
  text-align: center;
}
.imgSize {
  margin-left: 2%;
  margin-right: 2%;
  width: 3%;
  height: 30%;
}
.title {
  width: 35%;
  height: 6rem;
  line-height: 6rem;
  font-size: 1.8rem;
  margin-left: 8%;
}
.el_inp {
  margin-left: 10%;
  width: 30%;
}
.sous {
  width: 8%;
}
.dengr {
  width: 7%;
}
.el-icon-user {
  margin-right: 2%;
  margin-left: 2%;
  font-size: 1.8rem;
}

.layout {
  position: relative;
  left: 0;
  top: 0;
  display: flex;
  margin-top: 1rem;
  margin-left: 10%;
  width: 80%;
  text-align: center;
}
.layout a {
  flex: 1;
  text-decoration: none;
  padding: 1rem 0;
  line-height: 1.5rem;
  color: #000;
}
.layout a.router-link-active {
  background-color: rgb(149, 158, 170);
}
.layout a:hover {
  background-color: #627ecb;
}
</style>
